#include "videostream.h"
#include "videostream_rtsp.h"
#include <thread>

VStream::VStream()
{
}

VStream::~VStream()
{
}

void
VStream::startDoEventLoop()
{
	eventLoopWatchVariable = 0;
	m_pEnv->taskScheduler().doEventLoop(&eventLoopWatchVariable);
}

int 
VStream::OpenCamera(char const* rtspURL, char const* sUser, char const* sPasswd)
{
	m_pScheduler = BasicTaskScheduler::createNew();
	m_pEnv = BasicUsageEnvironment::createNew(*m_pScheduler);
	std::thread threadOnline(std::mem_fn(&VStream::startDoEventLoop), this);
	threadOnline.detach();
	m_pRtspClient = new VSRTSPClient(m_pEnv, rtspURL, sUser, sPasswd);
	if(m_pRtspClient) {
		return m_pRtspClient->open();
	}
	return VS_NULL;
}

int 
VStream::Close()
{
	if(m_pRtspClient) {
		m_pRtspClient->close();
		delete m_pRtspClient; m_pRtspClient = nullptr;
	}
	eventLoopWatchVariable = 1;
}

int 
VStream::GetFrameData(cv::Mat* frame)
{
	if(m_pRtspClient && m_pRtspClient->m_nState == VS_OK) {
		return m_pRtspClient->m_pFfmpegH264->GetDecodeFrame(frame);
	}
	return VS_NULL;
}

int 
VStream::State()
{
	eventLoopWatchVariable = 1;
	if(m_pRtspClient) {
		return m_pRtspClient->m_nState;
	}
	return VS_NULL;
}











